
<?php 
    $profile = View::get('profile'); 
    $user = View::get('user');
?>
<div class="title">Profil (<b><?php echo $user['login'] ?></b>)</div>

<div class="profil-box">
    <form action="<?php echo View::getRoot(); ?>/index.php?controller=user&action=profile_update" method="POST">
        <div class="dib">
            <div class="dib" style="width: 300px;">
                <label for="firstname"/>Data urodzenia:</label><Br/>
                <input type="text" id="i_birth_date" name="user[birth_date]" value="<?php echo $user['birth_date']; ?>"/>
            </div><br/>
            <div class="dib">
                <label for="sex"/>Płeć:</label>
                <select name="" name="data[sex]">
                    <option value="1" <?php if ($profile['sex']==1) echo "selected"; ?>>Meżczyzna</option>
                    <option value="0" <?php if ($profile['sex']==0) echo "selected"; ?>>Kobieta</option>
                </select> 
            </div>
            <br/><br/>
            <div id="flashContent"></div>
        </div>
        <div class="dib" style="width: 300px;">
            <label>O sobie</label><br/>
            <textarea cols="36" rows="7" name="data[about]"><?php echo $profile['about']; ?></textarea>
        </div>
        <br/>
        <div class="dib">
            <div class="dib">
                <label for="firstname"/>Imię:</label><Br/>
                <input type="text" name="data[firstname]" value="<?php echo $profile['firstname']; ?>"/>
                <Br/>
                <label for="lastname"/>Nazwisko</label><br/>
                <input type="text" name="data[lastname]" value="<?php echo $profile['lastname']; ?>"/><br/>
                
                <label for="email"/>Email</label><br/>
                <?php echo $user['email']; ?>
            
            </div>
            <div class="dib">

                <label for="webiste"/>Website:</label><br/>
                <input type="text" name="data[website]" value="<?php echo $profile['website']; ?>"/>
            <br/>
                <label for="skype"/>Skype:</label><br/>
                <input type="text" name="data[skype]" value="<?php echo $profile['skype']; ?>"/>
           <br/>
                <label for="gg"/>GG:</label><br/>
                <input type="text" name="data[gg]" value="<?php echo $profile['gg']; ?>"/>
            </div>
            
        </div>
        <div class="dib" style="margin-left:15px;">
            Zmień hasło<br/>
            <br/>           
            <label for="webiste"/>Stare hasło</label><br/>
            <input type="password" name="zm_old_password" id="zm_old_password"/><br/>
            <label for="webiste"/>Nowe hasło</label><br/>
            <input type="password" name="zm_password" id="zm_password"/><br/>
            <label for="webiste"/>Powtórz nowe hasło</label><br/>
            <input type="password" name="zm_password2" id="zm_password2"/>
            <input type="button" value="Zmień hasło" class="mysubmit" id="change-password"/>
        </div>

        <br/><br/>
        <div style="text-align: center;">
            <input type="submit" value="Uaktualnij" class="mysubmit" id="submit_button_profile"/>    
        </div>
    </form>
</div>

<script type="text/javascript">
    
    function isValidDate(s) {
          var matches = s.match("^([0-9]{4})-([0-9]{2})-([0-9]{2})$");
        
          if (!matches) return false;

          var composedDate = new Date(matches[1], (matches[2] - 1), matches[3]);

          return ((composedDate.getMonth() == (matches[2] - 1)) && (composedDate.getDate() == matches[3]) && (composedDate.getFullYear() == matches[1]));

    }
$(document).ready(function() {
    
    $('#submit_button_profile').bind('click', function(e) {
        e.preventDefault();
        
        if (!isValidDate($('#i_birth_date').val()))
        {
            alert('Nie prawidłowa data.');
            return false;
        }
        $('.profil-box form')[0].submit();
    });
    
    $('#change-password').live('click', function(e) {
        e.preventDefault();
        
        if ($('#zm_password').val().length <= 6)
        {
            alert('Hasło musi się składać z minimum 7 znaków.')
            return false;
        }
        
        if ($('#zm_password').val() != $('#zm_password2').val())
        {
            alert('Hasła są różne!');
            return false;
        }
        
        $.ajax({
          type: "POST",
          url: "<?php echo View::getRoot(); ?>/index.php?controller=user&action=change_password",
          data: {
              old_password: $('#zm_old_password').val(),
              password: $('#zm_password').val(),
              password2:$('#zm_password2').val()
          }
        }).done(function( msg ) {
              msg = JSON.parse(msg);
              alert(msg.message);
        });
    });
    
    swfobject.embedSWF("<?php echo View::getPath().'swf/'?>AvatarUploader.swf", 
    "flashContent", 
    "180px", "100px", "10.0.0","expressInstall.swf",
    {   script:"<?php echo View::getRoot(); ?>/index.php",
        imageSrc: "<?php echo View::getRoot(); ?>/../bank/avatar/<?php echo intval(UserModel::isLoggedIn()) ?>.jpg",
        normalHeight:'100',
        normalWidth:'100',    
        token:'1234qwerqwer'},     
    {wmode: 'transparent'}, 
    {id: 'flashDynamicContent'});
    
    $('#i_birth_date').datepicker({ 
        changeYear: true,
        setDate: $('#i_birth_date').val(),
        changeMonth: true,
        dateFormat: "yy-mm-dd",
        yearRange: '-100:+0'
    });
});
</script>
